/**
 * 
 * 上海金扳手信息科技股份有限公司
 * Copyright (c) 1991-2018 Jinbanshou,Inc.All Rights Reserved.
 */
package com.frames.table;

import java.util.Iterator;
import java.util.Map;

import com.utils.transtype.JsonObjectUtil;
import com.utils.transtype.TransType;

import net.sf.ezmorph.bean.MorphDynaBean;

/**
    * @ClassName: BeanTableUtil
    * @Description: 对象表转换工具
    * @author likai
    * @date 2017年5月7日
    *
    */
@Deprecated
public class NewBeanTableUtil {
	/**
	 * @throws Exception 
	* @Title: 
	* @Description: 根据参数值获取表列（获取表插入字段）
	* @param     参数
	* @return void    返回类型
	* @throws
	*/
	@SuppressWarnings("unchecked")
	public static String getColByParam(Object object) throws Exception {
		StringBuffer buffer=new StringBuffer();
		Map<String, Object> map=JsonObjectUtil.NON_NULL.beanToMap(object);
		Map<String, Object> data=JsonObjectUtil.NON_DEFAULT.beanToMap(object);//获取所有有值得属性和值
		String columns=(String) map.get("columns");
		Map<String,MorphDynaBean> clownsMap=TransType.jsonToObject(columns, Map.class);
		Iterator<String> keys=data.keySet().iterator();
		while (keys.hasNext()) {
			String key= keys.next();
			if (data.get(key)!=null&&clownsMap.get(key)!=null) {
				buffer.append(clownsMap.get(key));
				buffer.append(",");
			}
			
		}
		String result=buffer.toString().length()==0?"":buffer.toString().substring(0,buffer.length()-1);
		return result;
	}
	/**
	 * @throws Exception 
	* @Title: 
	* @Description: 根据参数值获取值（获取表插入值）
	* @param     参数
	* @return void    返回类型
	* @throws
	*/
	@SuppressWarnings("unchecked")
	public static String getValByParam(Object object,String paramName) throws Exception {
		StringBuffer buffer=new StringBuffer();
		
		Map<String, Object> map=JsonObjectUtil.NON_NULL.beanToMap(object);
		Map<String, Object> data=JsonObjectUtil.NON_DEFAULT.beanToMap(object);
		String columns=(String) map.get("columns");
		Map<String, MorphDynaBean> clownsMap=TransType.jsonToObject(columns, Map.class);
		Iterator<String> keys=data.keySet().iterator();
		while (keys.hasNext()) {
			String key= keys.next();
			if (data.get(key)!=null&&clownsMap.get(key)!=null) {
				buffer.append("#{");
				buffer.append(paramName);
				buffer.append(".");
				buffer.append(key);
				buffer.append("}");
				buffer.append(",");
			}
			
		}
		String result=buffer.toString().length()==0?"":buffer.toString().substring(0,buffer.length()-1);
		return result;
	}
	
	/**
	 * @throws Exception 
	* @Title: 
	* @Description: 根据参数值获取列名和值(获取表更新字段)
	* @param     参数
	* @return void    返回类型
	* @throws
	*/
	@SuppressWarnings("unchecked")
	public static String getColVal(Object object,String paramName,String id) throws Exception {
		StringBuffer buffer=new StringBuffer();
		Map<String, Object> map=JsonObjectUtil.NON_NULL.beanToMap(object);
		Map<String, Object> data=JsonObjectUtil.NON_DEFAULT.beanToMap(object);
		String columns=(String) map.get("columns");
		Map<String, MorphDynaBean> clownsMap=TransType.jsonToObject(columns, Map.class);
		Iterator<String> keys=data.keySet().iterator();
		while (keys.hasNext()) {
			String key= keys.next();
			if (data.get(key)!=null&&clownsMap.get(key)!=null&&!key.equals(id)) {
				buffer.append(clownsMap.get(key));
				buffer.append("=#{");
				buffer.append(paramName);
				buffer.append(".");
				buffer.append(key);
				buffer.append("}");
				buffer.append(",");
			}
			
		}
		String result=buffer.toString().length()==0?"":buffer.toString().substring(0,buffer.length()-1);
		return result;
	}
	public static void main(String[] args) {
		StringBuffer buffer=new StringBuffer();
		buffer.append(" like CONCAT('%', #{");
		buffer.append("user");
		buffer.append(".");
		buffer.append("mame");
		buffer.append("}) and ");
		System.err.println(buffer.toString());
	}
	/**
	 * @throws Exception 
	* @Title: 
	* @Description: 根据参数值获取条件（获取where条件,包含where字段）
	* @param     参数
	* @return void    返回类型
	* @throws
	*/
	@SuppressWarnings("unchecked")
	public static String getCondtionByParam(Object object,String paramName) throws Exception {
		StringBuffer buffer=new StringBuffer();
		Map<String, Object> map=JsonObjectUtil.NON_NULL.beanToMap(object);
		Map<String, Object> data=JsonObjectUtil.NON_DEFAULT.beanToMap(object);
		String columns=(String) map.get("columns") ;
		String colsAndWhere=(String) map.get("colsAndWhere") ;
		Map<String, String> clownsMap=TransType.jsonToObject(columns, Map.class);
		Map<String, MorphDynaBean> colsAndWhereMap=TransType.jsonToObject(colsAndWhere, Map.class);
		Iterator<String> keys=data.keySet().iterator();
		while (keys.hasNext()) {
			String key= keys.next();
			if (data.get(key)!=null&&clownsMap.get(key)!=null) {
				buffer.append(clownsMap.get(key));//获取表字段名
				String operate="=";//如果该列未配置操作，默认=
				if (colsAndWhereMap.get(key)!=null) {
					operate=(String) colsAndWhereMap.get(key).get("operate");
				}
				switch (operate) {
				case "lt%"://左边模糊匹配
					buffer.append(" like CONCAT('%', #{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("}) and ");
					break;
				case "rt%"://右边模糊匹配
					buffer.append(" like CONCAT( #{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("},'%') and ");
					break;
				case "%%"://全模糊匹配
					buffer.append(" like CONCAT( '%',#{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("},'%') and ");
					break;
				case "not"://非空匹配
					buffer.append(" IS NOT NULL and");
					break;
				case "null"://空值匹配
					buffer.append(" IS NULL and");
					break;
				case "in"://范围
					buffer.append(" in CONCAT( '(',#{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("},')') and ");
					break;

				default:
					//其他运算
					//=、<、<=、>、>=、!=
					buffer.append(operate);
					buffer.append("#{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("} and ");
					break;
				}
			}

		}
		String condition=buffer.toString();
		if (condition.equals("")) {
			return "";
		}else {
			return " where "+buffer.toString().substring(0, buffer.lastIndexOf(" and "));
		}
	}
	
	
	/**
	 * @throws Exception 
	* @Title: 
	* @Description: 根据参数值获取条件（获取where条件，不包含where字段,and开头）
	* @param     参数
	* @return void    返回类型
	* @throws
	*/
	@SuppressWarnings("unchecked")
	public static String getWhereCondtionByParam(Object object,String paramName) throws Exception {
		StringBuffer buffer=new StringBuffer();
		Map<String, Object> map=JsonObjectUtil.NON_NULL.beanToMap(object);
		Map<String, Object> data=JsonObjectUtil.NON_DEFAULT.beanToMap(object);
		String columns=(String) map.get("columns") ;
		String colsAndWhere=(String) map.get("colsAndWhere") ;
		Map<String, String> clownsMap=TransType.jsonToObject(columns, Map.class);
		Map<String, MorphDynaBean> colsAndWhereMap=TransType.jsonToObject(colsAndWhere, Map.class);
		Iterator<String> keys=data.keySet().iterator();
		while (keys.hasNext()) {
			String key= keys.next();
			if (data.get(key)!=null&&clownsMap.get(key)!=null) {
				buffer.append(clownsMap.get(key));//获取表字段名
				String operate="=";//如果该列未配置操作，默认=
				if (colsAndWhereMap.get(key)!=null) {
					operate=(String) colsAndWhereMap.get(key).get("operate");
				}
				switch (operate) {
				case "lt%"://左边模糊匹配
					buffer.append(" like CONCAT('%', #{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("}) and ");
					break;
				case "rt%"://右边模糊匹配
					buffer.append(" like CONCAT( #{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("},'%') and ");
					break;
				case "%%"://全模糊匹配
					buffer.append(" like CONCAT( '%',#{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("},'%') and ");
					break;
				case "not"://非空匹配
					buffer.append(" IS NOT NULL and");
					break;
				case "null"://空值匹配
					buffer.append(" IS NULL and");
					break;
				case "in"://范围
					buffer.append(" in CONCAT( '(',#{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("},')') and ");
					break;

				default:
					//其他运算
					//=、<、<=、>、>=、!=
					buffer.append(operate);
					buffer.append("#{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("} and ");
					break;
				}
			}

		}
		String condition=buffer.toString();
		if (condition.equals("")) {
			return "";
		}else {
			return " and "+buffer.toString().substring(0, buffer.lastIndexOf(" and "));
		}
	}
	/**
	 * @throws Exception 
	* @Title: 
	* @Description: 根据参数值获取条件,排除指定列remove（获取where条件，不包含where字段,and开头）
	* @param     参数
	* 				remove(','隔开)
	* @return void    返回类型
	* @throws
	*/
	@SuppressWarnings("unchecked")
	public static String getWhereCondtionRemove(Object object,String paramName,String remove) throws Exception {
		StringBuffer buffer=new StringBuffer();
		Map<String, Object> map=JsonObjectUtil.NON_NULL.beanToMap(object);
		Map<String, Object> data=JsonObjectUtil.NON_DEFAULT.beanToMap(object);
		String columns=(String) map.get("columns") ;
		String colsAndWhere=(String) map.get("colsAndWhere") ;
		Map<String, String> clownsMap=TransType.jsonToObject(columns, Map.class);
		Map<String, MorphDynaBean> colsAndWhereMap=TransType.jsonToObject(colsAndWhere, Map.class);
		Iterator<String> keys=data.keySet().iterator();
		while (keys.hasNext()) {
			String key= keys.next();
			if (data.get(key)!=null&&clownsMap.get(key)!=null&&!contain(remove, key, ",")) {
				buffer.append(clownsMap.get(key));//获取表字段名
				String operate="=";//如果该列未配置操作，默认=
				if (colsAndWhereMap.get(key)!=null) {
					operate=(String) colsAndWhereMap.get(key).get("operate");
				}
				switch (operate) {
				case "lt%"://左边模糊匹配
					buffer.append(" like CONCAT('%', #{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("}) and ");
					break;
				case "rt%"://右边模糊匹配
					buffer.append(" like CONCAT( #{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("},'%') and ");
					break;
				case "%%"://全模糊匹配
					buffer.append(" like CONCAT( '%',#{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("},'%') and ");
					break;
				case "not"://非空匹配
					buffer.append(" IS NOT NULL and");
					break;
				case "null"://空值匹配
					buffer.append(" IS NULL and");
					break;
				case "in"://范围
					buffer.append(" in CONCAT( '(',#{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("},')') and ");
					break;

				default:
					//其他运算
					//=、<、<=、>、>=、!=
					buffer.append(operate);
					buffer.append("#{");
					buffer.append(paramName);
					buffer.append(".");
					buffer.append(key);
					buffer.append("} and ");
					break;
				}
			}

		}
		String condition=buffer.toString();
		if (condition.equals("")) {
			return "";
		}else {
			return " and "+buffer.toString().substring(0, buffer.lastIndexOf(" and "));
		}
	}
	
	//判断key是否在字符串中
	public static boolean contain(String str,String key,String c) {
		String[] strs=str.split(c);
		for (int i = 0; i < strs.length; i++) {
			if (strs[i].equals(key)) {
				return true;
			}
		}
		return false;
	}
}
